<?php
//ini_set('display_errors', 'On');
include 'admin/base.php';

if ($_SERVER['SERVER_NAME'] != $GLOBALS['conf']['domain']) {
	redirect($GLOBALS['conf']['baseUrl'].'/login.php');
}

$op = Utility::getParam('op', 'login');

switch($op) {
	case 'login':
		$op();
		break;
	case 'doLogin':
		$op();
		break;
	default:
		login();
		break;
}

function login() {

	include $GLOBALS['ROOT_PATH'] . '/views/login.phtml';
}

function doLogin() {
	
	$username = Utility::getParam('username', '');
	$password = Utility::getParam('password', '');
	$userInfo = ObjectFinder::get("User")->checkPassword($username, $password);
	if ($userInfo) {
		$token = ObjectFinder::get("User")->encodeToken($userInfo['id'], $userInfo['username'], $userInfo['token']);
		setcookie("token", $token, 0, "/");
	    echoAjax(Constants::$ERR_SUCCESS);
	}else {
		echoAjax(Constants::$ERR_LOGIN_FAILED);
	}
}
?>